package org.mao.book;

import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

//我的春秋我做主
public class ReadBook2 {

	private Integer minPage = 100 * 0 + 10 * 0 + 1;

	private Integer maxPage = minPage + 0;

	private boolean isVip = true;

	private String authorName = "海棠栖露";

	public static void main(String[] arg) {
		try {
			ReadBook2 readBook = new ReadBook2();
			readBook.load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void load() throws Exception {
		for (int i = minPage; i <= maxPage; i++) {
			System.out.println("第" + i + "页,开始");
			read("http://bbs.tianya.cn/post-no05-381555-" + i + ".shtml");// #ty_vip_look[%E6%B5%B7%E6%A3%A0%E6%A0%96%E9%9C%B2]
			System.out.println("第" + i + "页,结束");
		}
	}

	public void read(String url) throws Exception {
		Document doc = Jsoup.connect(url).get();
		Elements elements = doc.getElementsByClass("atl-item");
		Iterator<Element> it = elements.iterator();
		while (it.hasNext()) {
			Element element = it.next();
			// System.out.println(element.text());
			Elements eles = element.getElementsByAttribute("uname");
			if (eles.iterator().hasNext()) {
				if (isVip && !authorName.equals(eles.iterator().next().text())) {
					continue;
				}
				System.out.println("===============================================================");
				System.out.println("作者：" + eles.iterator().next().text());
			}

			Elements eles2 = element.getElementsByClass("bbs-content");
			if (eles2.iterator().hasNext()) {
				System.out.println(parseContent(eles2.iterator().next().html()));
			}
		}
	}

	public String parseContent(String content) {
		content = content.replace("<br>", "");
		return content;
	}

}
